package com.atguigu02.reference;

import com.atguigu02.reference.data.Employee;
import org.junit.Test;

import java.util.Comparator;
import java.util.Locale;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 方法引用的使用
 *
 * Created by shkstart.
 */
public class MethodRefTest {

	// 情况一：对象 :: 实例方法
	//Consumer中的void accept(T t)
	//PrintStream中的void println(T t)
	@Test
	public void test1() {
		Consumer<String> consumer = new Consumer<String>() {
			@Override
			public void accept(String s) {
				System.out.println(s);
			}
		};

		consumer.accept("s");

		//lamda
		Consumer<String> consumer1 = str -> System.out.println(str);
		consumer1.accept("ssss");

		//方法引用
		Consumer<String> consumer2 = System.out::println;
		consumer2.accept("sssssssss");
		
	}
	
	//Supplier中的T get()
	//Employee中的String getName()
	@Test
	public void test2() {

		Employee emp = new Employee(1001, "马化腾", 34, 6000.38);
		Supplier<String> supplier = new Supplier<String>() {
			@Override
			public String get() {
				return emp.getName();
			}
		};

		System.out.println(supplier.get());

		Supplier<String> supplier1 = emp :: getName;
		System.out.println(supplier1.get());
	}

	// 情况二：类 :: 静态方法
	//Comparator中的int compare(T t1,T t2)
	//Integer中的int compare(T t1,T t2)
	@Test
	public void test3() {

		Comparator<Integer> comparator = Integer::compare;
		System.out.println(comparator.compare(12, 32));
	}
	
	//Function中的R apply(T t)
	//Math中的Long round(Double d)
	@Test
	public void test4() {

		Function<Double,Long> function = Math::round;

		System.out.println(function.apply(3.4));

	}

	// 情况三：类 :: 实例方法 
	// Comparator中的int comapre(T t1,T t2)
	// String中的int t1.compareTo(t2)
	@Test
	public void test5() {

		Comparator<String> comparator = String :: compareTo;

		comparator.compare("a","b");

	}

	//BiPredicate中的boolean test(T t1, T t2);
	//String中的boolean t1.equals(t2)
	@Test
	public void test6() {

		BiPredicate<String,String> predicate = String::equals;
		System.out.println(predicate.test("sss", "aaa"));
	}
	
	// Function中的R apply(T t)
	// Employee中的String getName();
	@Test
	public void test7() {
		Employee emp = new Employee(1001, "马化腾", 34, 6000.38);

		Function<Employee,String> function = new Function<Employee, String>() {
			@Override
			public String apply(Employee s) {
				return s.getName();
			}
		};
		Function<Employee,String> function1 = Employee::getName;

		System.out.println(function1.apply(emp));


	}

}
